/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       split_creatures_dialog.h

	$Header: /game/split_creatures_dialog.h $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "split_creatures_dialog.h"

#include "button_cache.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "bound_handler.h"
#include "button.h"
#include "combat_model_window.h"
#include "creature_traits.h"
#include "external_string.h"
#include "format_string.h"
#include "interface_help.h"
#include "platform.h"
#include "replace_keywords.h"
#include "scrollbar.h"
#include "standard_fonts.h"
#include "text_window.h"

static t_bitmap_group_cache g_layout( "dialog.split_creatures" );
extern t_button_cache g_ok_button;
extern t_button_cache g_cancel_button;
static t_external_string k_text_split_creatures( "split_creatures.dialog" );

// ------------------------------------------------------------------
// dialog for splitting one creature stack into 2 groups
// ------------------------------------------------------------------
t_split_creatures_dialog::t_split_creatures_dialog( t_creature_type creature_type, 
												    int amount_left, int amount_right,
													t_window* parent,
													int min_left, int min_right )
						: t_dialog_box( parent )
{
	m_creature_type = creature_type;
	m_amount = amount_right;
	m_maximum = amount_left + amount_right;
	m_min_amount = min_right;
	m_max_amount = m_maximum - min_left;

	if ( m_max_amount < m_min_amount )
		m_max_amount = m_min_amount;

	m_layout = g_layout.get();

	if ( m_amount < m_min_amount )
		m_amount = m_min_amount;
	else if ( m_amount > m_max_amount )
		m_amount = m_max_amount;

	/// add background
	t_screen_rect         rect;
	t_bitmap_layer const* layer;
	t_window*             window;

	layer = m_layout->find( "background" );
	window = new t_bitmap_layer_window( layer, t_screen_point(0,0), this );

	// add title
	std::string text;

	text = replace_keywords( k_text_split_creatures, "%creatures", 
							 get_traits( creature_type ).plural_name );
	add_text( text, "title" );

	rect = m_layout->find( "left_box" )->get_rect();
	window = new t_combat_model_window( rect, this, creature_type );
	rect = m_layout->find( "right_box" )->get_rect();
	window = new t_combat_model_window( rect, this, creature_type );

	// add source number
	m_source_number = add_text( format_string("%i", m_maximum - m_amount), 
		                           "source_number" );
	// add dest number
	m_dest_number = add_text( format_string( "%i", m_amount ), "dest_number" );
	
	// create scrollbar
	layer = m_layout->find( "scrollbar" );
	rect  = layer->get_rect();

	m_scrollbar = new t_scrollbar( rect.top_left(), rect.width(), this, m_min_amount, m_max_amount, false );
	m_scrollbar->set_position( m_amount );
	m_scrollbar->set_page_size( (m_max_amount - m_min_amount + 4) / 5 );
	m_scrollbar->set_handler( bound_handler( *this, &t_split_creatures_dialog::slider_change ));
	
	// add "ok" button
	t_help_block const&	   shared_help = get_help_block( "shared" );
	t_button* button;

	layer = m_layout->find( "ok_button" );
	button = new t_button( g_ok_button.get(), layer->get_rect().top_left(), this );
    set_help( button, shared_help, "ok" );
	add_close_button( button, 1 );

	// add "cancel" button
	layer = m_layout->find( "cancel_button" );
	button = new t_button( g_cancel_button.get(), layer->get_rect().top_left(), this );
    set_help( button, shared_help, "cancel" );
	add_close_button( button, 0 );

	// determine where to place window
	parent = get_parent();

	t_screen_rect  window_size = m_layout->find( "frame" )->get_rect();
	t_screen_point mouse_point = get_mouse_position( parent );

	// attempt to center over mouse location
	mouse_point.y -= window_size.height();
	mouse_point.x -= window_size.width() / 2;

	if (mouse_point.x < 0)
		mouse_point.x = 0;
	if (mouse_point.x + window_size.width() > parent->get_width())
		mouse_point.x = parent->get_width() - window_size.width();
	if (mouse_point.y < 0)
		mouse_point.y = 0;
	if (mouse_point.y + window_size.height() > parent->get_height())
		mouse_point.y = parent->get_height() - window_size.height();

	window_size += mouse_point - window_size.top_left();
	init( window_size );
}

// ------------------------------------------------------------------
// dialog for splitting one creature stack into 2 groups
// ------------------------------------------------------------------
t_text_window* t_split_creatures_dialog::add_text( std::string const& text, 
										 std::string const& layer_name )
{
	t_bitmap_layer const* layer;
	t_screen_rect         rect;
	t_text_window*        text_window;
	t_cached_font         font;

	layer = m_layout->find( layer_name );
	rect = layer->get_rect();
	font = get_font( rect.height() );
	text_window = new t_text_window( font, rect, this, text, t_pixel_24(0,0,0));
	text_window->set_center_horizontal();
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200));
	return text_window;
}

// ------------------------------------------------------------------
// dialog for splitting one creature stack into 2 groups
// ------------------------------------------------------------------
void t_split_creatures_dialog::slider_change( t_scrollbar* scrollbar, int value )
{
	if (m_amount == value)
		return;

	m_amount = value;
	m_source_number->set_text( format_string( "%i", m_maximum - m_amount ) );
	m_dest_number->set_text( format_string( "%i", m_amount ));
}
